package fr.cie.blog.beans;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Jee3
 * 
 */
public class Country extends AbstractModel {

	/**
	 * 
	 * countryCode.
	 */
	private String countryCode;

	/**
	 * cardList.
	 */
	private List<Card> cardList = new ArrayList<Card>();

	/**
	 * @return the cardList
	 */
	public final List<Card> getCardList() {
		return cardList;
	}

	/**
	 * @return the countryCode
	 */
	public final String getCountryCode() {
		return countryCode;
	}

	/**
	 * @param newCardList
	 *            the cardList to set
	 */
	public final void setCardList(final List<Card> newCardList) {
		this.cardList = newCardList;
	}

	/**
	 * @param newCountryCode
	 *            the countryCode to set
	 */
	public final void setCountryCode(final String newCountryCode) {
		this.countryCode = newCountryCode;
	}



	

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public final int hashCode() {
		final int prime = 31;
		int result = 1;

		if (cardList == null) {
			result = prime * result;
		} else {
			result = prime * result + cardList.hashCode();
		}

		if (countryCode == null) {
			result = prime * result;
		} else {
			result = prime * result + countryCode.hashCode();
		}

		if (getName() == null) {
			result = prime * result;
		} else {
			result = prime * result + getName().hashCode();
		}

		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public final boolean equals(final Object obj) {
		boolean result = true;
		if (this == obj) { // same object OK
			result = true;
		} else if (obj == null) { // object is null NOK
			result = false;
		} else if (!(obj instanceof Country)) { // object is not a Country NOK
			result = false;
		} else { // object is not the same, not null but a Country
			final Country other = (Country) obj;

			if (!equalsCountryCode(other)) {
				result = false;
			} else if (!equalsName(other)) {
				result = false;
			}
		}

		return result;
	}

	/**
	 * Check if names are the same.
	 * 
	 * @param other
	 *            the Country which contains the countryCode to compare
	 * @return true if the names are the same, false otherwise
	 */
	private boolean equalsName(final Country other) {
		boolean result = true;
		if (getName() == null) { // test on name
			if (other.getName() != null) {
				result = false;
			}
		} else if (!getName().equalsIgnoreCase(other.getName())) {
			result = false;
		}
		return result;
	}

	/**
	 * Check if countryCodes are the same.
	 * 
	 * @param other
	 *            the Country which contains the countryCode to compare
	 * @return true if the countryCodes are the same, false otherwise
	 */
	private boolean equalsCountryCode(final Country other) {
		boolean result = true;
		if (countryCode == null) {
			if (other.countryCode != null) {
				result = false;
			}
		} else if (!countryCode.equals(other.countryCode)) {
			result = false;
		}
		return result;
	}
}